package com.cloud.weichat.send.controller;

import com.cloud.weichat.CreateFileUtil;
import com.cloud.weichat.JsonFormatTool;
import com.cloud.weichat.MySVGConverter;
import com.cloud.weichat.WeiChatUtil;
import com.cloud.weichat.send.domain.WeiChat;
import com.cloud.weichat.send.domain.WeiChatMsg;
import com.cloud.weichat.send.domain.WeiSend;
import com.cloud.weichat.send.domain.WeiSendParam;
import com.cloud.weichat.send.service.IWeiSendService;
import net.sf.json.JSONObject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 企业微信发送的工具类
 */

@RestController
@RequestMapping("/weisend")
public class WeiSendController {

    @Resource
    private IWeiSendService weiSendService;

    @RequestMapping(value = "/queryWeiSend", method = RequestMethod.GET)
    public WeiSend queryWeiSend(HttpServletRequest request, Model model) throws Exception {

        String status = request.getParameter("status");
        WeiSend weiSend = weiSendService.queryWeiSend(status);
        return weiSend;

    }



    @RequestMapping(value = "/sendWeiXin", method = RequestMethod.GET)
    public HashMap<String, Object> weiSend(HttpServletRequest request, Model model) throws Exception {
        String svgcode = request.getParameter("svgcode");
        String status = "0";
        WeiSend weiSend = weiSendService.queryWeiSend(status);
        List<WeiSendParam> weiSendParamList = weiSendService.queryWeiSendParam(weiSend.getSend_id());

        HashMap<String,Object> map= new HashMap<String,Object>();
        map.put("userId",weiSend.getSend_user());
        map.put("titile",weiSend.getSend_title());
        map.put("summary",weiSend.getSend_summary());
        String users[] = weiSend.getSend_user().split(",");

        for (String userId: users) {
            WeiChatUtil.sendWeiChat(userId,weiSend.getSend_title(),weiSend.getSend_summary(),weiSend.getSend_url(),weiSend.getSend_picurl());
        }
        map.put("flag","success");
        return map;
    }
    @RequestMapping(value = "/weixinSend", method = RequestMethod.POST)
    public Object weixinSend(HttpServletRequest request, HttpServletResponse resp, Model model) throws Exception {

        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Methods","*");
        resp.setHeader("Access-Control-Max-Age","1000");
        request.setCharacterEncoding("UTF-8");
        String[] svg = request.getParameter("pic").split(",");
        String[] picNames = request.getParameter("picName").split(",");
        String[] users = request.getParameter("userId").split(",");
        String[] msg = request.getParameter("msg").split("@");
        String[] send_Title = request.getParameter("send_Title").split(",");
        String[] msg_column = request.getParameter("msg_column").split(",");

        String index_Title = request.getParameter("Index_Title");
        String index_Summary = request.getParameter("Index_Summary");
        String index_Url = request.getParameter("Index_Url");
        String index_Pic = request.getParameter("Index_Pic");
        String index_PicUrl = request.getParameter("Index_PicUrl");

        System.out.println("*******************index_Title="+index_Title);
        System.out.println("*******************index_Summary="+index_Summary);
        System.out.println("*******************msg_column="+msg_column[0]);


        HashMap<String,Object> map= new HashMap<String,Object>();
        if(svg.length!=picNames.length){
            map.put("error","101");
            map.put("flag","error");
            return map;
        }
        String downPath = request.getServletContext().getRealPath("/upload");
        String index_pic_name = String.valueOf(System.currentTimeMillis());

        MySVGConverter.writeImg(index_Pic, downPath,index_pic_name);

        for (int i = 0; i < svg.length; i++) {
            MySVGConverter.writeImg(svg[i], downPath,picNames[i]);
        }

        map.put("index_Title",index_Title);
        map.put("index_Summary",index_Summary);
        map.put("index_Pic",index_Pic);
//        map.put("msg_Title_Name",msg_Title_Name);
//        map.put("msg_Title_Value",msg_Title_Value);

        ArrayList<WeiChat> dList = new ArrayList<WeiChat>();
        for (int i = 0; i < send_Title.length; i++) {
            WeiChat chat = new WeiChat();
            String _msgs[] = msg[i].split("\\|");
            List<WeiChatMsg> chatMsgList = new ArrayList<WeiChatMsg>();
            for (int j = 0; j < _msgs.length; j++) {
                WeiChatMsg _weichatMsg = new WeiChatMsg();
                String _np[] = _msgs[j].split("#");
                _weichatMsg.setName(_np[0]);
                _weichatMsg.setPrice(_np[1]);
                chatMsgList.add(_weichatMsg);
            }
            chat.setMsg(chatMsgList);
            chat.setSend_Title(send_Title[i]);
            chat.setMsg_Title_Name(msg_column[i].split("#")[0]);
            chat.setMsg_Title_Value(msg_column[i].split("#")[1]);
            chat.setSvg(svg[i]);
            dList.add(chat);

        }
        map.put("chat",dList);
        map.put("error","0");
        map.put("flag","success");

        JSONObject json =  JSONObject.fromObject(map);
        String weichatJsonFile= index_pic_name;
        CreateFileUtil.createJsonFile(json.toString(),downPath,weichatJsonFile);


        //封装 indexUrl
        String index_PicPrefux  = index_PicUrl.substring(0,index_PicUrl.lastIndexOf("/")+1);
        index_PicUrl = index_PicPrefux+index_pic_name+".png";

        index_Url=index_Url+"?chatJsonFile="+weichatJsonFile;

        System.out.println("******index_PicUrl="+index_PicUrl);
        for (String userId: users) {
            WeiChatUtil.sendWeiChat(userId,index_Title,index_Summary,index_Url,index_PicUrl);
        }

        return "success";
    }


    @RequestMapping(value = "/queryWeiSendParam", method = RequestMethod.GET)
    public List<WeiSendParam> queryWeiSendParam(HttpServletRequest request, Model model) throws Exception {

        String send_id = request.getParameter("send_id");

        List<WeiSendParam> weiSendParamList = weiSendService.queryWeiSendParam(send_id);

        return weiSendParamList;

    }
    @RequestMapping(value = "/queryWeiChatParam", method = RequestMethod.POST)
    public String queryWeiChatParam(HttpServletRequest request,  HttpServletResponse resp,Model model) throws Exception {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        String downPath = request.getServletContext().getRealPath("/upload");
        String chatJsonFile = request.getParameter("chatJsonFile");
        org.json.JSONObject json = (org.json.JSONObject) JsonFormatTool.ReadJsonFile(downPath+ File.separator+chatJsonFile+".json");
        return json.toString();

    }

    public static void main(String[] args) {
        String t = "3|w";
        System.out.println(t.split("\\|").length);
        System.out.println(t.split("\\|")[0]);


    }

}
